package Pattern.StatePattern.Program.programone;

public class Door {
    public static final int CLOSED = 1;
    public static final int OPENING = 2;
    public static final int OPEN = 3;
    public static final int CLOSING = 4;
    public static final int STAYOPEN = 5;
    private int state = CLOSED;
    //定义状态变量，用不同的整数表示不同状态

    private void setState(int state) {
        this.state = state;  //设置传输门当前状态
    }

    public void getState() {
        //此处代码省略，本方法输出状态字符串，
        //例如，当前状态为CLOSED时，输出字符串为“CLOSED”
    }

    public void click() {
        if (state == CLOSED) setState(OPENING);//------------1
        else if (state==STAYOPEN) setState(CLOSING);//---------2
        else if (state ==OPEN) setState(STAYOPEN);//----------3
    }

    //发生timeout事件时进行状态转换
    public void timeout() {
        if (state == OPEN) setState(CLOSING);
    }

    public void complete() { //发生complete事件时进行状态转换
        if (state == OPENING) setState(OPEN);
        else if (state == CLOSING) setState(CLOSED);
    }

    public static void main(String[] args) {
        Door aDoor = new Door();
        aDoor.getState();
        aDoor.click();
        aDoor.getState();
        aDoor.complete();
        aDoor.getState();
        aDoor.click();
        aDoor.getState();
        aDoor.click();
        aDoor.getState();
        return;
    }
}
